﻿using System;
using System.IO;
using System.Net;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace Tavis.Formatters
{

    public class XmlFormatter : XmlMediaTypeFormatter
    {
        private readonly Parser _Parser;

        public enum Parser
        {
            XmlDocument,
            XDocument
        }

        public XmlFormatter(string xmlMediaType)
            : this()
        {
            SupportedMediaTypes.Add(new MediaTypeHeaderValue(xmlMediaType));
        }

        public XmlFormatter()
            : this(Parser.XmlDocument)
        {

        }

        public XmlFormatter(Parser parser)
        {
            _Parser = parser;
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xml"));
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/xml"));
            SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/xhtml+xml"));
        }


        protected override System.Threading.Tasks.Task<object> OnReadFromStreamAsync(Type type, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext)
        {
            return new TaskFactory<object>().StartNew(() =>
                                                          {
                                                              switch (_Parser)
                                                              {
                                                                  case Parser.XmlDocument:
                                                                      var doc = new XmlDocument();
                                                                      doc.Load(stream);
                                                                      return doc;
                                                                  case Parser.XDocument:
                                                                      return XDocument.Load(stream);
                                                              }
                                                              return null;
                                                          });
        }


        protected override Task OnWriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, FormatterContext formatterContext, TransportContext transportContext)
        {
            return new TaskFactory().StartNew(() =>
                                                          {
                                                              switch (_Parser)
                                                              {
                                                                  case Parser.XmlDocument:
                                                                      var doc = (XmlDocument)value;
                                                                      doc.Save(stream);
                                                                      break;
                                                                  case Parser.XDocument:
                                                                      var xdoc = (XDocument)value;
                                                                      xdoc.Save(stream);
                                                                      break;
                                                              }
                                                          });



        }
    }
}
